package com.sansecy.progressbar;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * @Description 进度条
 * @Date 2019-08-09 04:51
 * @Created by caoyu
 */
public class ProgressBar extends View {

    private int mBorderColor;
    private float mBorderWidth;
    private Paint mBorderPaint;
    private Paint mTextPaint;
    private int mProgress;
    private int mMaxProgress;
    private RectF mRectF;
    private String mText;
    private float mTextSize;
    private float mTextBaseLine;

    public ProgressBar(Context context) {
        this(context, null);
    }

    public ProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ProgressBar, 0, 0);
        mBorderColor = ta.getColor(R.styleable.ProgressBar_borderColor, Color.BLACK);
        mBorderWidth = ta.getDimension(R.styleable.ProgressBar_borderWidth, 5);
        mTextSize = ta.getDimension(R.styleable.ProgressBar_textSize, 12);

        mBorderPaint = new Paint();
        mBorderPaint.setStrokeWidth(mBorderWidth);
        mBorderPaint.setColor(mBorderColor);
        mBorderPaint.setStyle(Paint.Style.STROKE);
        mBorderPaint.setAntiAlias(true);

        mTextPaint = new Paint();
        mTextPaint.setColor(mBorderColor);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setAntiAlias(true);
        mRectF = new RectF();
        mRectF.set(100, 100, 500, 500);

        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        float textCenterLine = mRectF.top + mRectF.height() / 2;
        mTextBaseLine = PaintUtils.getBaseLine(fontMetrics, textCenterLine);
    }

    public void setTextSize(float size) {
        setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
    }

    public void setTextSize(int unit, float size) {
        mTextSize = TypedValue.applyDimension(unit, size, getResources().getDisplayMetrics());
    }

    public void setBorderColor(int borderColor) {
        mBorderColor = borderColor;
    }

    public void setBorderWidth(float borderWidth) {
        mBorderWidth = borderWidth;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float percentage = (float) mProgress / (float) mMaxProgress;
        int sweepAngle = (int) (360 * percentage);
        canvas.drawArc(mRectF, 180, sweepAngle, false, mBorderPaint);
        canvas.drawText(mText, mRectF.left + mRectF.width() / 2, mTextBaseLine, mTextPaint);

        if (isInEditMode()) {
            canvas.drawArc(mRectF, 180, 360, false, mBorderPaint);
            canvas.drawText(mText, mRectF.left + mRectF.width() / 2, mTextBaseLine, mTextPaint);
        }
    }

    public void start() {
        ValueAnimator valueAnimator = new ValueAnimator();
        valueAnimator.setDuration(3000);
        valueAnimator.setFloatValues(0f, 100f);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                setProgress((int) (value));
            }
        });
        valueAnimator.start();
    }

    public void setMaxProgress(int maxProgress) {
        mMaxProgress = maxProgress;
    }

    private void setProgress(int progress) {
        mProgress = progress;
        mText = "%" + progress;
        invalidate();
    }
}
